/*
 *     This file is part of Mobile GPS Logger.
 * 
 *     Mobile GPS Logger is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 * 
 *     Mobile GPS Logger is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License v3 for more details.
 * 
 *     You should have received a copy of the GNU General Public License v3
 *     along with Mobile GPS Logger. If not, see <http://www.gnu.org/licenses/>.
*/


package cx.ath.skyflyer.gpslog;

import cx.ath.skyflyer.gpslog.nmea.Time;
import cx.ath.skyflyer.gpslog.util.StringUtils;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

/**
 *
 * @author Ahti Legonkov
 */
public class LoggerForm extends Form {

    public LoggerForm(final Logger logger) {
        super("");
        setStatusText("Disconnected");

        append(m_GpsFixItem);
        append(m_GpsAltitude);
        append(m_GpsTime);
        append(m_GpsGSpeed);
        append(m_GpsVSpeed);
        
        addCommand(CMD_START);
        addCommand(CMD_CHART);
        addCommand(CMD_BACK);

        setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                if (c == CMD_STOP) {
                    logger.onCmdStop();
                }
                else if (c == CMD_START) {
                    logger.onCmdStart();
                }
                else if (c == CMD_PAUSE) {
                    logger.onCmdPause();
                }
                else if (c == CMD_RESUME) {
                    logger.onCmdResume();
                }
                else if (c == CMD_BACK) {
                    logger.onCmdBack();
                }
                else if (c == CMD_CHART) {
                    logger.onCmdChart();
                }
                else {
                    // ... ?
                }
            }
        });
    }

    protected void start(boolean startImmediately) {
        if (startImmediately)
            addCommand(CMD_PAUSE);
        else
            addCommand(CMD_START);
        removeCommand(CMD_START);
        addCommand(CMD_STOP);
        removeCommand(CMD_CHART);
        removeCommand(CMD_BACK);
    }

    protected void stop() {
        removeCommand(CMD_STOP);
        addCommand(CMD_START);
        removeCommand(CMD_PAUSE);
        removeCommand(CMD_RESUME);
        addCommand(CMD_CHART);
        addCommand(CMD_BACK);
        setStatusText("Disconnected");
    }

    protected void pause() {
        addCommand(CMD_RESUME);
        removeCommand(CMD_PAUSE);
        setStatusText("Paused");
    }

    protected void resume() {
        removeCommand(CMD_RESUME);
        addCommand(CMD_PAUSE);
        setStatusText("Logging");
    }

    private void setStatusText(String s) {
        if (s.length() > 0)
            setTitle(FORM_TITLE + ": " + s);
        else
            setTitle(FORM_TITLE);
    }

    public void setGpsAltitude(final double altitude, final String unit) {
        final String s = altitude + " " + unit.toLowerCase();
        m_GpsAltitude.setText(s);
    }

    public void setGpsTime(Time time) {
        if (time != null)
            m_GpsTime.setText(time.toString());
        else
            m_GpsTime.setText("N/A");
    }

    public void setGpsFixQuality(int fixQuality) {
        switch (fixQuality) {
        case 0:
            m_GpsFixItem.setText("0: No fix");
            break;

        case 1:
            m_GpsFixItem.setText("1: GPS fix");
            break;

        case 2:
            m_GpsFixItem.setText("2: DGPS fix");
            break;

        default:
            m_GpsFixItem.setText("" + fixQuality);
        }
    }

    public void setVerticalSpeed(double speed, String unit) {
        String s = StringUtils.toString(speed, 4, 2);
        m_GpsVSpeed.setText(s + " " + unit);
    }

    public void setGroundSpeed(double speed, String unit, String course) {
        m_GpsGSpeed.setText(speed + " " + unit + " " + course);
    }

    private static final String FORM_TITLE = "GPS Logger";
    private final StringItem m_GpsFixItem = new StringItem("Gps Fix", "N/A");
    private final StringItem m_GpsAltitude = new StringItem("Altitude", "N/A");
    private final StringItem m_GpsTime = new StringItem("Gps Time", "N/A");
    private final StringItem m_GpsGSpeed = new StringItem("Ground Speed", "N/A");
    private final StringItem m_GpsVSpeed = new StringItem("Vertical Speed", "N/A");

    /** A command to stop logging. */
    private static final Command CMD_STOP = new Command("Stop", Command.SCREEN, 1);

    /** A command to start logging. */
    private static final Command CMD_START = new Command("Start", Command.SCREEN, 1);

    /** A command to pause logging when logging is in progress. */
    private static final Command CMD_PAUSE = new Command("Pause", Command.SCREEN, 1);

    /** A command to resume logging when logging has been paused. */
    private static final Command CMD_RESUME = new Command("Resume", Command.SCREEN, 1);

    /** A command to go back to the previous page. */
    private static final Command CMD_BACK = new Command("Back", Command.BACK, 0);

    /** A command to view a chart of selected file. */
    private static final Command CMD_CHART = new Command("Chart", Command.CANCEL, 1);
}
